package com.journey.org.ui.mine.account

import android.app.Application
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import com.journey.org.BR
import com.journey.org.R
import com.journey.org.app.TitleViewModel
import com.journey.org.app.requestBase
import com.journey.org.app.requestHttpManager
import com.journey.org.entity.MineAccountEntity
import com.mvvmhabit.org.binding.command.BindingAction
import com.mvvmhabit.org.binding.command.BindingCommand
import com.mvvmhabit.org.binding.command.BindingConsumer
import com.mvvmhabit.org.bus.event.SingleLiveEvent
import me.tatarka.bindingcollectionadapter2.ItemBinding

class AccountViewModel(application: Application) : TitleViewModel(application) {

    override fun onCreate() {
        super.onCreate()
        onToolbarTitle.set("我的账号")
    }

    val isVisibleField = ObservableBoolean(false)
    val isClearVisibleField = ObservableBoolean(false)
    val onInputContentField = ObservableField<String>()

    val onSearchFocusChangeCommand = BindingCommand(object : BindingConsumer<Boolean> {
        override fun call(t: Boolean) {
            isClearVisibleField.set(t)
        }
    })

    val onSearchCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            if (onInputContentField.get() == "") {
                isVisibleField.set(false)
                return
            } else {
                isVisibleField.set(true)
                // 进行搜索
            }
        }
    })

    val onClearSearchCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            isClearVisibleField.set(false)
            onInputContentField.set("")
        }
    })

    val onLoadMoreCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            onPageField.set(onPageField.get() + 1)
            loadAccountList()
        }
    })

    val onRefreshCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            onPageField.set(1)
            loadAccountList()
        }
    })

    val onItemClickEvent = SingleLiveEvent<MineAccountEntity>()

    val itemBinding =
        ItemBinding.of<AccountItemViewModel>(BR.viewModel, R.layout.mine_account_item)

    val items = ObservableArrayList<AccountItemViewModel>()

    private val onPageField = ObservableInt(1)

    // 获取账户列表
    fun loadAccountList() {
        val params = HashMap<String, Any>()
        //  params["accountName"] = ""
        params["limit"] = "10"
        params["name"] = ""
        params["page"] = onPageField.get()
        params["queryMode"] = "page"
        if (onPageField.get() == 1) {
            showLoadDialog()
        }
        requestBase<List<MineAccountEntity>> {
            requestHttpManager {
                onLoader {
                    loadAccountList(params)!!
                }
                onSuccess {
                    if (it.isNotEmpty()) {
                        it.forEach { item ->
                            items.add(AccountItemViewModel(this@AccountViewModel, item))
                        }
                    }
                }
                onComplete {
                    dismissLoadDialog()
                }
            }
        }
    }
}